package org.leviatan.definator.gui.control;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import org.leviatan.definator.core.model.Field;
import org.leviatan.definator.core.model.Record;
import org.leviatan.definator.core.model.Table;
import org.leviatan.definator.gui.RuntimeContext;
import org.leviatan.definator.gui.swingmodels.FieldTreeNodeModel;
import org.leviatan.definator.gui.swingmodels.RecordTreeNodeModel;
import org.leviatan.definator.gui.swingmodels.TableTreeNodeModel;

public abstract class ListenerFactory {
	
	public static void initTreeSelectionListener(final JTree tree) {
		TreeSelectionListener theListener = 
			new TreeSelectionListener() {
				public void valueChanged(TreeSelectionEvent e) {
				    DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
	                	tree.getLastSelectedPathComponent();
	
					if (selectedNode == null) return;
					
					Object nodeModel = selectedNode.getUserObject();
					
					if (nodeModel instanceof TableTreeNodeModel) {
						TableTreeNodeModel tblNodeModel = (TableTreeNodeModel) nodeModel;
						String tblId = tblNodeModel.getId();
						//RuntimeContext.jTable.setModel(TableModelController.buildRecordTableModel(tblId));
						
						Table tblMdl = RuntimeContext.definatorService.getTableModel(tblId);
						RuntimeContext.singleEditViewPanel.displaySingleEditTablePanel(tblMdl);
						
						System.out.println("table" + tblNodeModel.getId());
					}
					else if (nodeModel instanceof RecordTreeNodeModel) {
						RecordTreeNodeModel recNodeModel = (RecordTreeNodeModel) nodeModel;
						String recId = recNodeModel.getId();
						//RuntimeContext.jTable.setModel(TableModelController.buildFieldTableModel(recId));
						
						Record recMdl = RuntimeContext.definatorService.getRecordModel(recId);
						RuntimeContext.singleEditViewPanel.displaySingleEditRecordPanel(recMdl);
						
						System.out.println(recNodeModel.getId());
						
					}
					else if (nodeModel instanceof FieldTreeNodeModel) {
						FieldTreeNodeModel fldNodeModel = (FieldTreeNodeModel) nodeModel;
						String fldId = fldNodeModel.getId();
						
						Field fldMdl = RuntimeContext.definatorService.getFieldModel(fldId);
						RuntimeContext.singleEditViewPanel.displaySingleEditFieldPanel(fldMdl);
						
						System.out.println(fldNodeModel.getId());
					}
					else return;
					
				}
				
			};
			tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
			tree.addTreeSelectionListener(theListener);
		
	}
	
	
	public static void initButDeleteListener(JButton button) {
		button.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent me) {
				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
            		RuntimeContext.jTree.getLastSelectedPathComponent();
				
				if (selectedNode == null) return;
				
				Object nodeModel = selectedNode.getUserObject();
				
				if (nodeModel instanceof FieldTreeNodeModel) {
					System.out.println("Delete field!");
					FieldTreeNodeModel fldModel = (FieldTreeNodeModel) nodeModel;
					RuntimeContext.definatorService.removeFieldModel(fldModel.getId());
					RuntimeContext.defTree.fieldNodeDeleted(fldModel.getId());
				}
				else if (nodeModel instanceof TableTreeNodeModel) {
					System.out.println("Delete table!");
					TableTreeNodeModel tblModel = (TableTreeNodeModel) nodeModel;
					RuntimeContext.definatorService.removeTableModel(tblModel.getId());
					RuntimeContext.defTree.tableNodeDeleted(tblModel.getId());
				}
				else if (nodeModel instanceof RecordTreeNodeModel) {
					System.out.println("Delete record!");
					RecordTreeNodeModel recModel = (RecordTreeNodeModel) nodeModel;
					RuntimeContext.definatorService.removeRecordModel(recModel.getId());
					RuntimeContext.defTree.recordNodeDeleted(recModel.getId());
				}
				RuntimeContext.definatorService.saveModels();
			}
		});
	}
	
	public static void initButCreateTblListener(JButton button) {
		button.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent me) {
				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
            		RuntimeContext.jTree.getLastSelectedPathComponent();
				
				RuntimeContext.singleEditViewPanel.displayNewTablePanel();
				
				if (selectedNode == null) return;
				
				//Object nodeModel = selectedNode.getUserObject();
				
			}
		});
		
	}
	
	public static void initButCreateFldListener(JButton button) {
		button.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent me) {
				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
            		RuntimeContext.jTree.getLastSelectedPathComponent();
				
				if (selectedNode == null)
					RuntimeContext.singleEditViewPanel.displayNewFieldPanel(null);
				else {
					Object nodeModel = selectedNode.getUserObject();
					
					if (nodeModel instanceof RecordTreeNodeModel) {
						RecordTreeNodeModel recMdl = (RecordTreeNodeModel) nodeModel;
						RuntimeContext.singleEditViewPanel.displayNewFieldPanel(recMdl.getId());
					}
					else
						RuntimeContext.singleEditViewPanel.displayNewFieldPanel(null);
					
				}
				
			}
		});
		
	}
	
	public static void initButCreateRecListener(JButton button) {
		button.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent me) {
				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
            		RuntimeContext.jTree.getLastSelectedPathComponent();
				
				if (selectedNode == null)
					RuntimeContext.singleEditViewPanel.displayNewRecordPanel(null);
				else {
					Object nodeModel = selectedNode.getUserObject();
					
					if (nodeModel instanceof TableTreeNodeModel) {
						TableTreeNodeModel tblMdl = (TableTreeNodeModel) nodeModel;
						RuntimeContext.singleEditViewPanel.displayNewRecordPanel(tblMdl.getId());
					}
					else
						RuntimeContext.singleEditViewPanel.displayNewRecordPanel(null);
					
				}
				
			}
		});
	}
	
	

}
